/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.resource;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

import com.mycompany.emailsenderdemo.IEmailService;
import com.mycompany.emailsenderdemo.vo.EmailVO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * REST Web Service
 *
 * @author Jeff
 */
@Path("generic")
@Component
public class GenericResource {
    private static final Logger LOG = Logger.getLogger(GenericResource.class.getName());
    @Resource
    IEmailService commonsEmailService;

    @Context
    private UriInfo context;

    /** Creates a new instance of GenericResource */
    public GenericResource() {
    }

    /**
     * Retrieves representation of an instance of com.mycompany.resource.GenericResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/plain")
    public String getText() {
        //TODO return proper representation object
//        throw new UnsupportedOperationException();
        commonsEmailService.sendMail("Commons Test Email Content");
        
//        sendCommonsMail();
//        sendSpringMail();
        
        
        return "Executed";
    }

    /**
     * PUT method for updating or creating an instance of GenericResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("text/plain")
    public void putText(String content) {
    }
    
    private void sendCommonsMail() {
        try {
            Email email = new SimpleEmail();
            email.setHostName("smtp.gmail.com");
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator("zhou.zhefu", "5642326"));
            email.setTLS(true);
            email.setFrom("zhou.zhefu@gmail.com");
            email.setSubject("TestMail");
            email.setMsg("This is a test mail ... :-)");
            email.addTo("jeff2033333@yahoo.com.sg");
            email.send();
        } catch (EmailException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }

}
